package com.ssb.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 黄河接口对接-获取支付流水号
 */
@Component
public class HhePolicyTradeNo extends HheSuperPlugin {

    @Autowired
    private CheckedFrontSysService checkedFrontSysService;

    @Override
    public Object beforeInvoke(Map<String, Object> map) {

        //校验此前端系统是否存在且有效
        String frontSysCode = StrUtil.doNull(String.valueOf(map.get("frontSysCode")));
        Map<String,String> result =  checkedFrontSysService.checkedSysByFrontSysCode(frontSysCode);
        if (result.get("errorCode").equals("F")){
            throw new BusinessException("此系统有误：" + result.get("errorMsg"));
        }

        String infoId=String.valueOf(map.get("infoId"));
        if(StrUtil.isNull(infoId)){
            throw new BusinessException("订单ID不能为空");
        }
        Object obj = getOrderById(infoId);
        if(!Validator.isEmpty(obj)){
            map.put("orderInfo",obj);
            customMethod(map);
        }
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        customMethod(map);
        return null;
    }

    private void customMethod(Map<String, Object> map){
        long startLong = System.currentTimeMillis();
        List<Map> list = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        DataMap orderInfo = (DataMap)map.get("orderInfo");
        param.put("businessno",orderInfo.get("requestNo"));//投保单号
        param.put("payway","2101");//支付方式:2101 保融收款,210 刷卡
        param.put("msppayflag","E");//系统标识：A 移动端 B 渠道 C快速出单 D 冠创 E 柿柿保
        list.add(param);

        String str = String.valueOf(sendInterface("Externalcommon/tradeNoCommon", list,null));
        log.info("获取支付流水号结果："+str);
        if(Validator.isNotNull(str)){//处理接口返回结果
            JSONArray array = JSON.parseObject(str,JSONArray.class);
            for(int i=0;i<array.size();i++){
                JSONObject json = array.getJSONObject(i);
//                "businessNo": "06201001360201700000010330",
//                "flag": "1",
//                "tradeNo": "01620000201706000171"
                String status="";
                if("1".equals(json.getString("flag"))){
                    status="order_status='4',";
                }
                String sql = "update ss_insur_order set "+status+" trade_no='"+json.getString("tradeNo")+"' where info_id='"+orderInfo.get("infoId")+"'";
                IusPluginUtil.getInstance().saveInvoke(DATA_SOURCE,sql);
                map.put("tradeNo",json.getString("tradeNo"));
            }
            map.put("resultData",array);
        }

        log.info("huanghe interface exetime = "+(System.currentTimeMillis()-startLong)+" ms");
    }


}
